from elasticsearch import Elasticsearch

# 创建 Elasticsearch 客户端
es = Elasticsearch(["http://localhost:9200"])  # 替换成你的 Elasticsearch 地址

#
# # 删除索引
es.indices.delete(index='es_qiniu')
#
# """索引名 es_python,ignore=400，表示忽视400这个错误，如果存在es_python时，会返回400"""
# create = es.indices.create(index='es_qiniu', ignore=400)

# es.search(index='es_qiniu')  # index：选择数据库

# 提前映射各字段类型创建
mapping = {
    "mappings": {
        "properties": {
            "title": {
                "type": "text",
                "analyzer": "ik_max_word"
            },
            "id": {
                "type": "integer",
            }
        }
    }
}

print(es.indices.create(index="es_qiniu", body=mapping))

es.create(index="es_qiniu", id='20', body={"title": "我爱中国共产党", "id": 20})

print(es.search(index='es_qiniu', body={"query": {"match": {"title": "中国"}}}))
